require 'ftools'

end_glue_path = File.join('src', 'end_glue')
end_asset_path = 'asset'
_checkout_paths = ['end_graphics', 'end_system', 'end_builder'].map { |item| File.join('trunk', item) }
checkout_path_map = Hash[*_checkout_paths.collect { |dir| [dir, File.join(end_glue_path, 'core', File.basename(dir))] }.flatten]
checkout_path_map['assets/nori7'] = 'asset'

desc "console"
task :console do
	# Run a console to communicate with the process forked by 'rake run_game'
	# e.g.
	# >>> world = World()
	# >>> world.append(Entity("player"))
	# >>> world.append(Entity("enemy"))
	sh 'nohup rake run_game>/dev/null&'
	sh 'python src/scripts/console.py'
end

desc "update"
task :update do
	core_path = File.join(end_glue_path, 'core')
	core_entries = (Dir.entries(core_path) - ['..', '.', '.svn']).map { |item| File.join(core_path, item) }
	core_dirs = core_entries.reject { |item| not File.directory?(item) }
	sh 'svn update -r' + (ENV['nori7_rev'] or 'HEAD')
	sh "svn update -r#{(ENV['end_rev'] or 'HEAD')} " + core_dirs.join(' ') + " " + end_asset_path
end

desc "create links to END"
task :end_link, [:end_dir] do |t, args|
	args.with_defaults(:end_dir => '/usr/share/end')
	ln_sh = []

	end_dir = args.end_dir

	if not File.directory?(end_dir)
		puts end_dir + " is not directory"
		exit(127)
	end

	checkout_path_map.each do |target, dest|
		target = File.join(end_dir, target)
		if not File.exists?(target)
			puts target + " is not exists."
			break
		end
		ln_sh.push(['ln -s', target, dest].join(' '))
	end

	ln_sh.each do |sh_command|
		sh sh_command
	end
end

desc "checkout END"
task :end_checkout, [:end_svnroot] do |t, args|
	args.with_defaults(:end_svnroot => 'http://kldp.net/svn/end')
	checkout_path_map.each do |target, dest|
		sh ['svn checkout', File.join(args.end_svnroot, target), dest].join(' ')
	end
end

desc "checkout END third_party (for win32)"
task :end_checkout_third_party, [:end_svnroot] do |t, args|
	args.with_defaults(:end_svnroot => 'http://kldp.net/svn/end')
	['wxWidgets-2.8.9', 'Python25'].each do |dir|
		sh ['svn checkout', File.join(args.end_svnroot, 'third_party', dir), File.join(end_glue_path, 'third_party', dir)].join(' ')
	end
end

desc "export END third_party dll (for win32)"
task :end_export_dll, [:end_svnroot] do |t, args|
	args.with_defaults(:end_svnroot => 'http://kldp.net/svn/end')
	['wxWidgets-2.8.9', 'Python25'].each do |dir|
		sh ['svn checkout', File.join(args.end_svnroot, 'third_party', dir), File.join(end_glue_path, 'third_party', dir)].join(' ')
	end
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/cygwxmsw28d_gcc_custom-0.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/cygwxmsw28d_gl_gcc_custom-0.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/python25.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/python25_d.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28_net_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28_odbc_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28_xml_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28d_net_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28d_odbc_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28d_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxbase28d_xml_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_adv_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_aui_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_core_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_gl_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_html_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_media_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_qa_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_richtext_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28_xrc_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_adv_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_aui_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_core_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_gl_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_html_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_media_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_qa_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_richtext_vc_custom.dll'
	sh 'svn export ' + args.end_svnroot + '/trunk/runtime/wxmsw28d_xrc_vc_custom.dll'
end

desc "run game"
task :run_game do
	require File.join(end_glue_path, 'core/end_builder/end_default_builder')
	FileUtils.cd 'src/end_glue/core'

	ProjectBuilderGCC.new( E_SYSTEM, true ).build
	ProjectBuilderGCC.new( E_GRAPHICS, true ).build

	custom_include_options = Array.new
	custom_include_options.push('-I ../embed')
	custom_lib_options = Array.new
	if RUBY_PLATFORM =~ /cygwin/
		custom_include_options.push('-I/usr/include/python2.5')
		custom_lib_options.push('-L../third_party/Python25/cygwin').push('-lpython2.5.dll')
	else
		custom_include_options.push(`python-config --includes`.gsub(/\n/, ' '))
		custom_lib_options.push(`python-config --libs`.gsub(/\n/, ' '))
	end

	# set cpp part info of this project
	cpp_part_project_info = CustomProject.new('../../../', 'nori7', true)
	cpp_part_project_info.setPreDependencies(['../embed/*.h', E_GRAPHICS_LIB, E_SYSTEM_LIB])
	cpp_part_project_info.setCompileFiles(['../../main.cpp', '../embed/*.cpp', 'end_system/*Shell*.cpp'])
	cpp_part_project_info.setExcludedFiles(E_DEFAULT_EXCLUDE)
	cpp_part_project_info.setIncludeFlag(E_GRAPHICS_INCLUDE)
	cpp_part_project_info.setProjectTypeFlag('_wx_gl_custom_')

	# build project
	ProjectBuilderGCC.new(E_CUSTOM_PROJECT, true).build_from_project_info(cpp_part_project_info.getInfos, custom_include_options.join(' '), custom_lib_options.join(' '))

	#run
	cpp_part_project_info.execProjectNameFile
end

require 'rake/clean'
CLEAN.include('end_glue/core/*.o', 'end_glue/core/*.a', '*_gcc', '*_gcc_g', '*_gcc.exe', '*_gcc_g.exe')
